/**
 * 报表信息展示相关JS
 */

$(function () {
    // 查询数据源信息
    $.ajax({
        url:basePath + "/rptTools/saveRpt/queryDBCfg",
        type:'POST',
        dataType:'json',
        async: false,
        success:function(data){
            $.each(data.dbCfgs, function (index, obj) {
                var dbId = obj.sid;
                var dbName = obj.dbName;
                $("#dbSource").append("<option value='" + dbId + "'>" + dbName + "</option>");
            });

            $.each(data.accessIns, function (index, obj) {
                var sysCode = obj.sysCode;
                var sysName = obj.sysName;
                $("#accessIn").append("<option value='" + sysCode + "'>" + sysName + "</option>");
            })
        }
    });

    // 初始化多选下拉框
    $('.multiSelect').fSelect();

    // 初始化表格
    var dg = $('#dg').datagrid({
        url: basePath + "/rptTools/rptMgr/queryRptInfo",
        singleSelect: true,
        rownumbers:true,
        striped: true,
        remoteSort: true,
        pagination:true,//显示分页
        pageSize: 20,
        //pageList: [500,1000,2000],
        columns:[
            [
                {field:'rptId',title:'报表ID',width:80},
                {field:'rptToken',title:'报表TOKEN',hidden:true},
                {field:'rptName',title:'报表名称',width:350,formatter:function(value,row,index){
                    return '<a style="color:#4697e5; text-decoration:none;" onClick="index.showRptInfo(\'' + row.rptToken + '\',' +  row.rptId + ')" href="#">'+row.rptName+'</a>';
                }},
                {field:'dbName',title:'报表数据源',width:150},
                {field:'sysCode',title:'接入系统',width:150,hidden:true},
                {field:'sysName',title:'接入系统',width:150},
                {field:'operate',title:'操作',width:400,align:'center',formatter:function(value,row,index){
                    return '<a style="color:#4697e5; text-decoration:none;padding-right:15px;" onClick="index.downLoadXML(' +  row.rptId + ',\'' + row.rptName + '\')" href="#">'+row.operate+'</a>'
                        + '<a style="color:#4697e5; text-decoration:none;padding-right:15px;" onClick="index.editRpt(' +  row.rptId + ',\'' + row.rptName + '\',\'' + row.sysCode + '\',\'' + row.dbName + '\')" href="#">修改报表</a>'
                        + '<a style="color:#4697e5; text-decoration:none;padding-left:30px;" onClick="index.downLoadExcel(' +  row.rptId + ',\'' + row.rptName + '\')" href="#">下载表头Excle</a>'
                        + '<a style="color:#4697e5; text-decoration:none;padding-left:15px;" onClick="index.delRpt(\'' +  row.rptName + '\',' + row.rptId + ')" href="#">删除报表</a>';
                }}
            ]
        ]
    });
});

var index = {
    /**
     * 查询数据
     */
    query : function() {
        var rptName = $('#rptName').val();  // 报表名称
        var dbValues = $('#dbSource').val();  // 报表数据源
        var accessIns = $('#accessIn').val();  // 接入系统

        var param = {};
        param.rptName = rptName;
        param.dbValues = dbValues;
        param.accessIns = accessIns;
        $('#dg').datagrid({
            url:basePath + "/rptTools/rptMgr/queryRptInfo",
            queryParams:param
        });
    },
    addRpt : function () {
        window.open(basePath + "/rptTools/rptMgr/addRpt");
    },
    showRptInfo : function (rptToken,rptId) {
        rptId = rptId + "encode";
        rptId = this.encode64(rptId);
        rptId = this.encode64(rptId);
        var url = basePath + "/rptTools/showRpt/showRptIndex?rptId=" + rptId
        if(rptToken == 'undefined' || rptToken == '') {
            window.open(url);
        } else {
            url = url + "&token=" + rptToken;
            window.open(url);
        }
    },
    refresh : function () {
        this.showMask();
        $.ajax({
            url:basePath + "/rptTools/rptMgr/refreshCache",
            type:'POST',
            dataType:'json',
            async: true,
            success:function(data){
                $("#mask").hide();
                var status = data.status;
                if(status == "1") {
                    alert("刷新缓存成功");
                } else {
                    alert("刷新缓存失败");
                }
            }
        })
    },
    editRpt : function (rptId,rptName,sysCode,dbName) {
        var encodName = encodeURI(encodeURI(rptName));
        var encodeDb = encodeURI(encodeURI(dbName));
        window.open(basePath + "/rptTools/rptMgr/editRpt?rptId=" + rptId + "&rptName=" + encodName + "&dbName=" + encodeDb + "&sysCode=" + sysCode);
    },
    delRpt : function (rptName,rptId) {
        var confirmMsg = '确认删除' + rptName + '吗?';
        $.messager.confirm('提示', confirmMsg, function(r){
            if (r){
                $.ajax({
                    url:basePath + "/rptTools/rptMgr/deleteRpt",
                    type:'POST',
                    dataType:'json',
                    data:{
                        rptId: rptId
                    },
                    async: false,
                    success:function(data){
                        var status = data.status;
                        if(status == "1") {
                            alert("报表删除成功");
                            $('#dg').datagrid({
                                url:basePath + "/rptTools/rptMgr/queryRptInfo"
                            });
                        } else {
                            alert("报表删除失败");
                        }
                    }
                })
            }
        });
    },
    downLoadXML : function (rptId, rptName) {
        var name = encodeURI(encodeURI(rptName));
        window.open(basePath + "/rptTools/rptMgr/downLoadRptCfg?rptId=" + rptId + "&rptName=" + name);
    },
    downLoadExcel : function (rptId, rptName) {
        var name = encodeURI(encodeURI(rptName));
        window.open(basePath + "/rptTools/rptMgr/downLoadRptHeader?rptId=" + rptId + "&rptName=" + name);
    },
    showMask : function () {
        $("#mask").css("height",$(document).height());
        $("#mask").css("width",$(document).width());
        $("#mask").show();
    },
    encode64: function(input) {
        input = input + "";
        var keyStr = "ABCDEFGHIJKLMNOP" + "QRSTUVWXYZabcdef" + "ghijklmnopqrstuv" + "wxyz0123456789+/" + "=";
        var output = "";
        var chr1, chr2, chr3 = "";
        var enc1, enc2, enc3, enc4 = "";
        var i = 0;
        do {
            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);
            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;
            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }
            output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2)
                + keyStr.charAt(enc3) + keyStr.charAt(enc4);
            chr1 = chr2 = chr3 = "";
            enc1 = enc2 = enc3 = enc4 = "";
        } while (i < input.length);
        return output;
    }
}
